﻿using System;
using System.Globalization;
using System.Runtime.Serialization;
using System.Security.Permissions;
using Istace.Framework.Mapping.Core.Interfaces;

namespace Istace.Framework.Mapping.Core.Exceptions
{
    /// <summary>
    /// Exception thrown when user try to add an object to a repository which is already registred
    /// </summary>
    [Serializable]
    public class MappedObjectAlreadyIndexedException<T> : RepositoryBaseException<T>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MappedObjectAlreadyIndexedException{T}"/> class.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="indexedInstance">The indexed instance.</param>
        /// <param name="objectIndex">Index of the object.</param>
        /// <param name="targetRepositoryInstance">The target repository instance.</param>
        public MappedObjectAlreadyIndexedException(string message, object indexedInstance, string objectIndex, IIndexedRepository<T> targetRepositoryInstance) : base(message, indexedInstance, objectIndex, targetRepositoryInstance)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MappedObjectAlreadyIndexedException{T}"/> class.
        /// </summary>
        public MappedObjectAlreadyIndexedException()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MappedObjectAlreadyIndexedException{T}"/> class.
        /// </summary>
        /// <param name="message">The message.</param>
        public MappedObjectAlreadyIndexedException(string message) : base(message)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MappedObjectAlreadyIndexedException{T}"/> class.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="exception">The exception.</param>
        public MappedObjectAlreadyIndexedException(string message, Exception exception) : base(message, exception)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MappedObjectAlreadyIndexedException{T}"/> class.
        /// </summary>
        /// <param name="info">The info.</param>
        /// <param name="context">The context.</param>
        protected MappedObjectAlreadyIndexedException(SerializationInfo info, StreamingContext context) : base(info, context)
        {
        }

        /// <summary>
        /// The object which was already mapped
        /// </summary>
        private T NewObject { get; set; }

        /// <summary>
        /// Default .ctor
        /// </summary>
        /// <param name="key">The key used to index the already mapped object</param>
        /// <param name="alreadyIndexedInstance">The already indexed objet which is stored</param>
        /// <param name="newAlreadyMappedObject">The object already indexed which need to be added</param>
        /// <param name="targetRepository">The repository which throw the exception</param>
        public MappedObjectAlreadyIndexedException(string key, T alreadyIndexedInstance, T newAlreadyMappedObject, IIndexedRepository<T> targetRepository)
            : base(String.Format(CultureInfo.InvariantCulture, "The current object is already index on the key {0} in the {1} type repository", key, typeof(T).FullName),
                    alreadyIndexedInstance, 
                    key, 
                    targetRepository)
        {
            this.NewObject = newAlreadyMappedObject;
        }

        /// <summary>
        /// When overridden in a derived class, sets the <see cref="T:System.Runtime.Serialization.SerializationInfo" /> with information about the exception.
        /// </summary>
        /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo" /> that holds the serialized object data about the exception being thrown.</param>
        /// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext" /> that contains contextual information about the source or destination.</param>
        /// <PermissionSet>
        ///   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Read="*AllFiles*" PathDiscovery="*AllFiles*" />
        ///   <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="SerializationFormatter" />
        ///   </PermissionSet>
        [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            if(info == null) throw new ArgumentNullException("info");
            info.AddValue("NewObject", NewObject);
            base.GetObjectData(info, context);
        }
    }
}
